<?php
    class iJMenu{
        /**
         *
         * This function is iJMenu construct
         *
         * @access public
         */
        public function __construct() {
        }
        
        
        public function createMenuItem($item) {
          
          if(empty($item['url']) || !isset($item['url'])) {
            iJMessages::display('fail', 'fail', 'url_is_undefined');
            return;
          } else {
            $urls = iJHrefLink::getAllUrls();
            if($item['type'] == 'inner') {
              if(!array_key_exists($item['url'], $urls)) {
                iJMessages::display('fail', 'fail', 'url_is_undefined');
              }
            }
          }
          
          if(empty($item['type']) || !isset($item['type'])) {
            iJMessages::display('fail', 'fail', 'type_is_undefined');
            return;
          }
          
          if(empty($item['sortOrder']) || !isset($item['sortOrder'])) {
            $item['sortOrder'] = 1;
          }
          
          if(empty($item['parent']) || !isset($item['parent'])) {
            $item['parent'] = 0;
          }
          
          $iJLanguages =  iJFactory::singleton('iJLanguages'); 
          $languages  = $iJLanguages->getLanguages();
          foreach($languages as $lang) {
             if(empty($item['title_' . $lang['code']])){
              iJMessages::display('fail', 'fail', 'empty_title');
              return;
             }
             if(!iJValidation::Unex_symb($item['title_' . $lang['code']])){
               iJMessages::display('fail', 'fail', 'Incorrect name');
               return;
            }      
          }
          
          

           if (!iJValidation::number($item['sortOrder'])){
             iJMessages::display('fail', 'fail', 'Incorrect name');
             return;
           }     
          
          //TODO POST request check
          $iJLanguages =  iJFactory::singleton('iJLanguages');
          $iJDataBase  = iJFactory::singleton('iJDataBase');
          $QMenu = $iJDataBase->query('INSERT INTO :table_admin_menu 
                                                    (
                                                        parrent_id,
                                                        linkType,
                                                        url,
                                                        sortOrder
                                                    )
                                                    VALUES 
                                                    (
                                                        :parrent_id,
                                                        :linkType,
                                                        :url,
                                                        :sortOrder
                                                        
                                                    )');
          $QMenu->bindTable(':table_admin_menu',  TABLE_ADMIN_MENU);
          $QMenu->bindValue(':url',         $item['url']);
          $QMenu->bindValue(':parrent_id',  $item['parent']);
          $QMenu->bindValue(':sortOrder',   $item['sortOrder']);
          $QMenu->bindValue(':linkType',    $item['type']);
          $QMenu->execute();
          if(!$iJDataBase->isError()) {
            $id = $iJDataBase->getLastInsertId();
            $languages = $iJLanguages->getLanguages();
            foreach($languages as $lang) {
              if(isset($item['title_' . $lang['code']])) {
                  $definition = $item['title_' . $lang['code']];
              } else {
                  $definition = 'unset';
              }
              $iJLanguages->addDefinition(TABLE_ADMIN_MENU, $id, $lang['code'], $definition);
            }
            if(!$iJDataBase->isError()) {
              $iJDataBase->commitTransaction();
              iJMessages::display('success', 'menu_item_created');
            } else {
              iJMessages::display('fail', 'fail');
              $iJDataBase->rollbackTransaction();
            }
          } else {
            iJMessages::display('fail', 'fail');
            $iJDataBase->rollbackTransaction();
          }
          
        }
        
        
         public function editMenuItem($item) {
         
          if(empty($item['url']) || !isset($item['url'])) {
            iJMessages::display('fail', 'fail', 'url_is_undefined');
            return;
          } else {
            $urls = iJHrefLink::getAllUrls();
            if($item['type'] == 'inner') {
              if(!array_key_exists($item['url'], $urls)) {
                iJMessages::display('fail', 'fail', 'url_is_undefined');
              }
            }
          }
          
          if(empty($item['type']) || !isset($item['type'])) {
            iJMessages::display('fail', 'fail', 'type_is_undefined');
            return;
          }
          
          if(empty($item['sortOrder']) || !isset($item['sortOrder'])) {
            $item['sortOrder'] = 1;
          }
       
          if (!iJValidation::Unex_symb($item['title'])){
             iJMessages::display('fail', 'fail', 'Incorrect name');
             return;
          }         
           if (!iJValidation::number($item['sortOrder'])){
             iJMessages::display('fail', 'fail', 'Incorrect name');
             return;
          }        
             /*
          if (!iJValidation::name($blockInfo['CSS'], 0)){
             iJMessages::display('fail', 'fail', 'incorrect_CSS_name');
             return;
          }
          if (!iJValidation::name($blockInfo['JS'], 0)){
             iJMessages::display('fail', 'fail', 'incorrect_JS_name');
             return;
          }        
          
          if (!iJValidation::name($blockInfo['class'], 0)){
             iJMessages::display('fail', 'fail', 'incorrect_class');
             return;
          }
          
          if (!iJValidation::name($blockInfo['language'])){
             iJMessages::display('fail', 'fail', 'incorrect_language');
             return;
          }
          
          if (!iJValidation::name($blockInfo['description'], 0)){
             iJMessages::display('fail', 'fail', 'incorrect_description');
             return;
          }*/
          
          $iJLanguages =  iJFactory::singleton('iJLanguages');
          $iJDataBase  = iJFactory::singleton('iJDataBase');
          $QMenu = $iJDataBase->query('UPDATE :table_admin_menu 
                                      SET linkType  = :linkType,
                                          sortOrder = :sortOrder,
                                          url       = :url
                                      WHERE id = :id');
          $QMenu->bindTable(':table_admin_menu',  TABLE_ADMIN_MENU);
          $QMenu->bindValue(':url',         $item['url']);
          $QMenu->bindValue(':id',          $item['id']);
          $QMenu->bindValue(':sortOrder',   $item['sortOrder']);
          $QMenu->bindValue(':linkType',    $item['type']);
          $QMenu->execute();
       //   spr($QMenu); die();
          if(!$iJDataBase->isError()) {
            
            $iJLanguages->addDefinition(TABLE_ADMIN_MENU, $item['id'], $iJLanguages->getLangCode(), $item['title']);
            if(!$iJDataBase->isError()) {
              $iJDataBase->commitTransaction();
              iJMessages::display('success', 'menu_item_changed');
            } else {
              iJMessages::display('fail', 'fail');
              $iJDataBase->rollbackTransaction();
            }
          } else {
            iJMessages::display('fail', 'fail');
            $iJDataBase->rollbackTransaction();
          }
          
        }
        
        
        public function removeMenuItem($itemId) {
            //TODO GC
            $iJLanguages =  iJFactory::singleton('iJLanguages');
            $iJDataBase  =  iJFactory::singleton('iJDataBase');
            
            $QBlocks = $iJDataBase->query("DELETE FROM :table_blocks
                                                 WHERE id = :id");
            $QBlocks->bindTable(':table_blocks', TABLE_ADMIN_MENU);
            $QBlocks->bindValue(':id', $itemId, 'int');
            $QBlocks->execute();
            
            $iJLanguages->removeDefinitions(TABLE_BLOCKS, $itemId);
            
            if(!$iJDataBase->isError()) {
              iJMessages::display('success', 'deleted');
            } else {
              iJMessages::display('fail', 'fail');
            }
            
        }
        
 
    }
?>